package com.feeyo.groundservice.zxing

import android.Manifest
import android.content.res.ColorStateList
import android.graphics.Color
import android.view.Gravity
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.graphics.toColorInt
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ScreenUtils
import com.feeyo.groundservice.base_util.getColor
import com.feeyo.groundservice.base_util.getPxFromResource
import com.feeyo.groundservice.base_util.getRouterPath
import com.feeyo.groundservice.base_util.toastCenter
import com.feeyo.groundservice.zxing.constant.ZxingConstant
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.Size

/**
 *作者：70893 on 2025-07-31
 *描述：扫码工具类
 *
 *
 *
 * 如果callback 为空，可以使用Eventbus，订阅事件 ScanCodeResultEvent[com.feeyo.groundservice.zxing.event.ScanCodeResultEvent]
 */
enum class ScanCodeType {
    /**条码**/
    BARCODE,

    /**二维码**/
    QRCODE,

    /**混合**/
    MIXED,

    /**混合连续**/
    MIXED_CONTINUOUS,
}

object ScanCodeUtil {

    /**扫码**/
    fun scanCode(type: ScanCodeType, title: String? = null, callback: ScanCodeListener? = null) {
        //请求权限
        PermissionUtils.permission(Manifest.permission.CAMERA, Manifest.permission.VIBRATE)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    when (type) {
                        ScanCodeType.BARCODE -> {
                            scanOnlyBarCode(title, callback)
                        }

                        ScanCodeType.QRCODE -> {
                            scanOnlyQRCode(title, callback)
                        }

                        ScanCodeType.MIXED -> {
                            scanSingleCode(title, callback)
                        }

                        ScanCodeType.MIXED_CONTINUOUS -> {
                            scanContinueCode(title, callback)
                        }

                        else -> {
                            toastCenter("暂不支持的类型")
                        }
                    }
                }

                override fun onDenied() {
                    toastCenter("允许相机权限才能扫码")
                }
            }).request()
    }


    /**单独扫条码的**/
    private fun scanOnlyBarCode(title: String? = null, callback: ScanCodeListener? = null) {
        ZxingConstant.OnlyBarCodeCaptureActivity.getRouterPath()
            .withString(ZxingConstant.TITLE, title)
            .apply {
                if (callback != null) {
                    withObject(ZxingConstant.CALLBACK, callback)
                }
            }
            .navigation()
    }

    /**单独扫二维码的**/
    private fun scanOnlyQRCode(title: String? = null, callback: ScanCodeListener? = null) {
        ZxingConstant.OnlyQrCaptureActivity.getRouterPath()
            .withString(ZxingConstant.TITLE, title)
            .apply {
                if (callback != null) {
                    withObject(ZxingConstant.CALLBACK, callback)
                }
            }
            .navigation()
    }

    /**单次扫码，各种类型**/
    private fun scanSingleCode(title: String? = null, callback: ScanCodeListener? = null) {
        ZxingConstant.SingleCaptureActivity.getRouterPath()
            .withString(ZxingConstant.TITLE, title)
            .apply {
                if (callback != null) {
                    withObject(ZxingConstant.CALLBACK, callback)
                }
            }
            .navigation()
    }

    /**连续扫码，各种类型**/
    private fun scanContinueCode(title: String? = null, callback: ScanCodeListener? = null) {
        ZxingConstant.ContinuousCaptureActivity.getRouterPath()
            .withString(ZxingConstant.TITLE, title)
            .apply {
                if (callback != null) {
                    withObject(ZxingConstant.CALLBACK, callback)
                }
            }
            .navigation()
    }
}


/**扫描结果**/
interface ScanCodeListener {
    fun onScanCodeSuccess(result: String,format:String)
}